package com.suao.fruits.service.impl;

import com.suao.common.annotation.DataScope;
import com.suao.common.constant.DelFlagConst;
import com.suao.common.utils.DateUtils;
import com.suao.common.utils.SecurityUtils;
import com.suao.common.utils.StringUtils;
import com.suao.fruits.domain.FruitsInfo;
import com.suao.fruits.domain.FruitsSalesInfo;
import com.suao.fruits.mapper.FruitsSalesInfoMapper;
import com.suao.fruits.service.IFruitsInfoService;
import com.suao.fruits.service.IFruitsSalesInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 水果销售流水Service业务层处理
 *
 * @author tyz
 * @date 2023-02-27
 */
@Service
public class FruitsSalesInfoServiceImpl implements IFruitsSalesInfoService
{
    @Autowired
    private FruitsSalesInfoMapper fruitsSalesInfoMapper;
    
    @Autowired
    private IFruitsInfoService fruitsInfoService;

    /**
     * 查询水果销售流水
     *
     * @param id 水果销售流水主键
     * @return 水果销售流水
     */
    @Override
    public FruitsSalesInfo selectFruitsSalesInfoById(Long id)
    {
        return fruitsSalesInfoMapper.selectFruitsSalesInfoById(id);
    }

    /**
     * 查询水果销售流水列表
     *
     * @param fruitsSalesInfo 水果销售流水
     * @return 水果销售流水
     */
    @DataScope(userAlias = "u")
    @Override
    public List<FruitsSalesInfo> selectFruitsSalesInfoList(FruitsSalesInfo fruitsSalesInfo)
    {
        return fruitsSalesInfoMapper.selectFruitsSalesInfoList(fruitsSalesInfo);
    }

    /**
     * 新增水果销售流水
     *
     * @param fruitsSalesInfo 水果销售流水
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFruitsSalesInfo(FruitsSalesInfo fruitsSalesInfo)
    {
        fruitsSalesInfo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        fruitsSalesInfo.setCreateTime(DateUtils.getNowDate());
        fruitsSalesInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        fruitsSalesInfo.setUpdateTime(DateUtils.getNowDate());
        int result = fruitsSalesInfoMapper.insertFruitsSalesInfo(fruitsSalesInfo);
        if(result>0)
        {
            //减掉库存
            FruitsInfo fruitsInfo = fruitsInfoService.selectFruitsInfoById(fruitsSalesInfo.getFruitsId());
            if(StringUtils.isNotNull(fruitsInfo))
            {
                BigDecimal inventory = fruitsInfo.getInventory().subtract(fruitsSalesInfo.getSalesNum());
                BigDecimal inventoryTotal = fruitsInfo.getInventoryTotal().subtract(fruitsSalesInfo.getSalesNum());
                fruitsInfo.setInventory(inventory);
                fruitsInfo.setInventoryTotal(inventoryTotal);
            }
            fruitsInfoService.updateFruitsInfo(fruitsInfo);
        }
        return result;
    }

    /**
     * 修改水果销售流水
     *
     * @param fruitsSalesInfo 水果销售流水
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFruitsSalesInfo(FruitsSalesInfo fruitsSalesInfo)
    {
        //1.获取原始销售数量
        FruitsSalesInfo old = fruitsSalesInfoMapper.selectFruitsSalesInfoById(fruitsSalesInfo.getId());
        BigDecimal oldSalesNum = old.getSalesNum();
        fruitsSalesInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        fruitsSalesInfo.setUpdateTime(DateUtils.getNowDate());
        int result = fruitsSalesInfoMapper.updateFruitsSalesInfo(fruitsSalesInfo);
        if(result>0)
        {
            //减掉库存
            FruitsInfo fruitsInfo = fruitsInfoService.selectFruitsInfoById(fruitsSalesInfo.getFruitsId());
            if(StringUtils.isNotNull(fruitsInfo))
            {
                //先把库存加回去再减掉最新的销售数量
                BigDecimal inventory = fruitsInfo.getInventory().add(oldSalesNum).subtract(fruitsSalesInfo.getSalesNum());
                BigDecimal inventoryTotal = fruitsInfo.getInventoryTotal().add(oldSalesNum).subtract(fruitsSalesInfo.getSalesNum());
                fruitsInfo.setInventory(inventory);
                fruitsInfo.setInventoryTotal(inventoryTotal);
            }
            fruitsInfoService.updateFruitsInfo(fruitsInfo);
        }
        return result;
    }

    /**
     * 批量删除水果销售流水
     *
     * @param ids 需要删除的水果销售流水主键
     * @return 结果
     */
    @Override
    public int deleteFruitsSalesInfoByIds(Long[] ids)
    {
        int count =0;
        for(Long id:ids)
        {
            updateFruitsSalesInfoDelFlag(id);
            count++;
        }
        return count;
    }
    

    /**
     * 删除水果销售流水信息
     *
     * @param id 水果销售流水主键
     * @return 结果
     */
    @Override
    public int deleteFruitsSalesInfoById(Long id)
    {
        updateFruitsSalesInfoDelFlag(id);
        return 1;
    }
    
    @Transactional
    public void  updateFruitsSalesInfoDelFlag(Long id)
    {
        FruitsSalesInfo temp = fruitsSalesInfoMapper.selectFruitsSalesInfoById(id);
        temp.setDelFlag(DelFlagConst.DELETED);
        temp.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        temp.setUpdateTime(DateUtils.getNowDate());
        fruitsSalesInfoMapper.updateFruitsSalesInfo(temp);
        
        //+回库存
        FruitsInfo fruitsInfo = fruitsInfoService.selectFruitsInfoById(temp.getFruitsId());
        if(StringUtils.isNotNull(fruitsInfo))
        {
            //先把库存加回去再减掉最新的销售数量
            BigDecimal inventory = fruitsInfo.getInventory().add(temp.getSalesNum());
            BigDecimal inventoryTotal = fruitsInfo.getInventoryTotal().add(temp.getSalesNum());
            fruitsInfo.setInventory(inventory);
            fruitsInfo.setInventoryTotal(inventoryTotal);
        }
        fruitsInfoService.updateFruitsInfo(fruitsInfo);
    }
}
